﻿$(document).ready(function () {
    "use strict";
    var iscreate = true;
    var opt = {
        controls: [{
            name: "usermodal",
            type: "modal",
            body: [
                {
                    type: "gridtable",
                    name: "modalgriduser"
                }
            ]
        },
            {
                type: "row",
                controls: [{
                    type: "header", title: "Material Management", breadcrumb: "mstbarang",
                    toolbarbutton: [
                                { type: "btn", name: "btnCreate", text: "create", color: "blue", icon: "plus", onClick: create },
                                { type: "btn", name: "btnEdit", text: "Edit", color: "green", icon: "edit", onClick: edit },
                                { type: "btn", name: "btnDelete", text: "Delete", color: "red", icon: "times", onClick: deleted },
                                { type: "btn", name: "btnSave", text: "Save", color: "purple", icon: "save", cls: "hide", onClick: save },
                                { type: "btn", name: "btnCancel", text: "Cancel", color: "yellow", icon: "undo", cls: "hide", onClick: cancel }
                    ]
                }
                ]
            },
        {
            type: "row",
            controls: [
                {
                    type: "portlet", name: "pnlfilter", color: "green", title: "List Material", icon: "home", hidetool: ["remove", "config"],
                    body: [
                        {
                            type: "formgroup",
                            controls: [
                                   { name: "fltrKdBrg", type: "textbox", label: "Kode Barang", cls: "col-md-3", placeholder: "Kode Barang"  }
                            
                            ]
                        },
                            {
                                type: "formgroup",
                                controls: [
                                    { name: "fltrNmBrg", type: "textbox", label: "Nama Barang", cls: "col-md-5", placeholder: "Nama barang" }
                                ]
                            },
                            {
                                type: "formgroup",
                                controls: [
                                        { name: "fltrBCode", type: "textbox", label: "Barcode",  cls: "col-md-3", placeholder: "Barcode" }
                                ]
                            },
                            {
                                type: "formgroupbtn",
                                controls: [
                                        { name: "btnSearch", type: "btn", color: "blue", text: "Search", icon: "search" ,onClick:refreshgrid}

                                ]
                            },

                        {
                            type: "gridtable",
                            name: "gridbrg"
                        }
                    ]

                }
            ]
        },
        {
            type: "row",
            controls: [
                {
                    type: "portlet",mustvalid:true, name: "pnlInput", color: "purple", title: "Manage Material", icon: "home", hidetool: ["remove", "config"], hidden: true,
                    body: [
                            {
                                type: "formgroup",
                                controls: [
                                       {
                                           name: "KdBrg", type: "textbox", label: "Kode Barang",
                                           clslabel: "col-md-3", cls: "col-md-5", placeholder: "Kode Barang",
                                           txtcls:"UCase",required: true, minlength: 5
                                       }
                                ]
                            },
                            {
                                type: "formgroup",
                                controls: [
                                        {
                                            name: "NmBrg", type: "textbox", label: "Nama Barang", clslabel: "col-md-3",
                                            txtcls:"TCase", cls: "col-md-5", placeholder: "Nama Barang", required: true
                                        }
                                ]
                            },
                            {
                                type: "formgroup",
                                controls: [
                                        {
                                            name: "BCode", type: "textbox", label: "Barcode",number:true,
                                            txtcls: "LCase", clslabel: "col-md-3", cls: "col-md-5",
                                            placeholder: "Barcode", required: true
                                            
                                        }
                                ]
                            },
                            {
                                type: "formgroup",
                                controls: [
                                        {
                                            name: "TypeBrg", type: "dropdownlist", label: "label",
                                            title: "Satuan", clslabel: "col-md-3", cls: "col-md-5",
                                            dfltText: "-- SELECT ONE --", required: true
                                        }
                                ]
                            },
                            {
                                type: "formgroup",
                                controls: [
                                        {
                                            name: "UnitId", type: "dropdownlist", label: "label",
                                            title: "Satuan", clslabel: "col-md-3", cls: "col-md-5",
                                            dfltText: "-- SELECT ONE --", required: true
                                        }
                                ]
                            },
                            {
                                type: "formgroup",
                                controls: [
                                        {
                                            name: "MinStock", type: "textbox", label: "Minimal Stock",
                                            clslabel: "col-md-3", cls: "col-md-5", txtcls: "numeric",
                                            placeholder: "Stok Minimal", required: true
                                        }
                                ]
                            },
                            {
                                type: "formgroup",
                                controls: [
                                        {
                                            name: "InitStock", type: "textbox", label: "Init Stock",
                                            clslabel: "col-md-3", cls: "col-md-5", number: true,
                                            txtcls: "numeric", placeholder: "Stock", required: true, maxlength: 7
                                        }
                                ]
                            }
                    ]
                }]
        }
        ]
    }

    var widget = new PageEngine(opt);
    widget.render(init);
    function searchemployee() {
        refreshsrcemployee();
    }


    function init() {
        widget.PopulateCombo({ nmspc: "unit", name: "UnitId" })
        widget.PopulateCombo({ nmspc: "typebrg", name: "TypeBrg" })
        refreshgrid();
    }

   
    function create() {
        iscreate = true;
        widget.clearcontrol("#pnlInput");
        widget.showonlyportlet("pnlInput");
        widget.showbuttontoolbar("#btnSave,#btnCancel");
        widget.readonly({ name: "#KdBrg", readonly: false });
    }

    function edit() {       
        var rows = $("#gridbrg").dataTable().fnGetNodes();
        var cb = $("#gridbrg input:checked");
        if (cb.length == 0) {
            widget.showmessage([{ type: "error", msg: "please select data first", title: "error edit" }]);
        }
        else if (cb.length > 1) {
            widget.showmessage([{ type: "error", msg: "multiple edit is not active yet", title: "error edit" }])
        }
        else {
            editbarang(cb.val());
        }
    }
    function deleted() {
        var rslt = widget.submit(widget.getbaseurl() + 'scp/mdm/barang/delete',
            { cb: JSON.stringify(widget.getSelectedGridRow("gridbrg")) }, true);
        if (rslt.success) {
            widget.showmessage([{ msg: "data has been deleted", title: "Done", type: "success" }]);
        }
        refreshgrid();
    }   

    function save() {
        if (!widget.isvalid()) {            
        }
        else {
            var data = $("#pnlInput").serializeObject();
            data.iscreate = (iscreate ? iscreate : 0);
            var rslt = widget.submit(widget.getbaseurl() + 'scp/mdm/barang/save', data, true);
            if (rslt.success) {
                widget.showmessage([{ msg: "data saved successfully", title: "Done", type: "success" }]);
            }
            else {
                widget.showmessage([{ msg: "Failed to Save: " + rslt.message, title: "Error", type: "error" }]);
            }
            widget.showonlyportlet('pnlfilter');
            widget.showbuttontoolbar("#btnCreate,#btnEdit,#btnDelete");
            refreshgrid();
        }
    }

    function cancel() {
        widget.showonlyportlet("pnlfilter");
        widget.showbuttontoolbar("#btnCreate,#btnEdit,#btnDelete");
    }


    function editbarang(data) {
        iscreate = false;
        widget.clearcontrol("#pnlInput");
        widget.populatedata({ url: "api/mdm/apibrg/mstbarang/" + data }, "#pnlInput");
        widget.showonlyportlet("pnlInput");
        widget.showbuttontoolbar("#btnSave,#btnCancel");
        widget.readonly({ name: "#KdBrg" });
    }

    function refreshgrid() {
        var data = $("#pnlfilter").serializeObject();
        widget.reloadgrid({
            url: "api/mdm/grid/barang",
            name: "gridbrg",
            checkbox: true,
            params:data,
            key: "KdBrg",
            filter: true,
            columns: [
            { mData: "KdBrg", sTitle: "Kode Barang " },
            { mData: "NmBrg", sTitle: "Nama Barang" },
            { mData: "BCode", sTitle: "Barcode" },
            { mData: "TypeBrgDsc", sTitle: "Tipe Barang" },
            { mData: "MinStock", sTitle: "Min. Stok" },
            { mData: "InitStock", sTitle: "Stok" },
            { mData: "UnitIdDsc", sTitle: "Satuan" }
            ],
            rwdblclick: function (data) {
                editbarang(data[1]);
            }
        });

    }



});